package com.webemail.sys.service.impl;

import com.alibaba.fastjson2.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.webemail.sys.mapper.SysMessageDraftsMapper;
import com.webemail.sys.model.SysMessageDrafts;
import com.webemail.sys.service.MessageDraftsService;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * 用户管理
 */
@Service
public class MessageDraftsServiceImpl extends ServiceImpl<SysMessageDraftsMapper,SysMessageDrafts> implements MessageDraftsService{

	private Logger logger = LoggerFactory.getLogger(getClass());

	@Resource
	public SysMessageDraftsMapper sysMessageDraftsMapper;

	@Override
	public int insert(SysMessageDrafts temp) {
		// TODO Auto-generated method stub
		logger.info("class:" + this.getClass().getName() + " method:" + "insert" + " param:" + JSON.toJSONString(temp));
		return sysMessageDraftsMapper.insert(temp);
	}

	@Override
	public int delete(long id, long userId) {
		logger.info("class:" + this.getClass().getName() + " method:" + "delete" + " param:" + "id:" + id + "userId:"
				+ userId);
		return sysMessageDraftsMapper.delete(Wrappers.lambdaQuery(SysMessageDrafts.class)
				.eq(SysMessageDrafts::getId,id)
				.eq(SysMessageDrafts::getUserId,userId));
	}

	@Override
	public int update(SysMessageDrafts temp) {
		// TODO Auto-generated method stub
		logger.info("class:" + this.getClass().getName() + " method:" + "update" + " param:" + JSON.toJSONString(temp));
		return sysMessageDraftsMapper.updateById(temp);
	}

	@Override
	public SysMessageDrafts get(long id, long userId) {
		// TODO Auto-generated method stub
		logger.info("class:" + this.getClass().getName() + " method:" + "get" + " param:" + "id:" + id + "userId:"
				+ userId);
		SysMessageDrafts temp = new SysMessageDrafts();
		temp.setId(id);
		temp.setUserId(userId);
		return sysMessageDraftsMapper.selectOne(Wrappers.lambdaQuery(SysMessageDrafts.class)
				.eq(SysMessageDrafts::getId,id)
				.eq(SysMessageDrafts::getUserId,userId));
	}

	@Override
	public List<SysMessageDrafts> list(long mailId, long userId) {
		// TODO Auto-generated method stub
		logger.info("class:" + this.getClass().getName() + " method:" + "list" + " param:" + "emailId:" + mailId
				+ "userId:" + userId);
		return sysMessageDraftsMapper.selectList(Wrappers.lambdaQuery(SysMessageDrafts.class)
				.eq(SysMessageDrafts::getMailId,mailId)
				.eq(SysMessageDrafts::getUserId,userId));
	}

	@Override
	public boolean insert(List<SysMessageDrafts> temp) {
		logger.info("class:" + this.getClass().getName() + " method:" + "insert" + " param:" + JSON.toJSONString(temp));
		return this.saveBatch(temp);
	}

	@Override
	public List<SysMessageDrafts> listByPage(long userId, long mailId, String colomn, String word, int offset,
			int limit, int... states) {
		// TODO Auto-generated method stub
		logger.info("class:" + this.getClass().getName() + " method:" + "listByPage" + " param:" + "mailId:" + mailId
				+ "userId:" + userId);
		List<Integer> list = new ArrayList<Integer>();
		for (int i = 0; i < states.length; i++) {
			list.add(states[i]);
		}
		LambdaQueryWrapper<SysMessageDrafts> wrapper =Wrappers.lambdaQuery(SysMessageDrafts.class)
		.eq(SysMessageDrafts::getUserId,userId)
				.eq(SysMessageDrafts::getMailId,mailId)
				.in(SysMessageDrafts::getState,list);
		if (StringUtils.isNotBlank(colomn) && StringUtils.isNotBlank(word)) {
			colomn = colomn.trim();
			word = word.trim();
			if ("subject".equalsIgnoreCase(colomn)) {
				wrapper.like(SysMessageDrafts::getSubject,word);
			} else if ("bodyText".equalsIgnoreCase(colomn)) {
				wrapper.like(SysMessageDrafts::getBodyText,word);
			} else if ("mailFrom".equalsIgnoreCase(colomn)) {
				wrapper.like(SysMessageDrafts::getMailFrom,word);
			} else if ("mailAddress".equalsIgnoreCase(colomn)) {
				wrapper.like(SysMessageDrafts::getMailAddress,word);
			}
		}
		Page<SysMessageDrafts> page = new Page<>();
		page.setCurrent(offset);
		page.setSize(limit);
		return sysMessageDraftsMapper.selectPage(page,wrapper).getRecords();
	}

	@Override
	public long count(long userId, long mailId, String colomn, String word, int... states) {
		List<Integer> list = new ArrayList<>();
		for (int i = 0; i < states.length; i++) {
			list.add(states[i]);
		}
		LambdaQueryWrapper<SysMessageDrafts> wrapper =Wrappers.lambdaQuery(SysMessageDrafts.class)
				.eq(SysMessageDrafts::getUserId,userId)
				.eq(SysMessageDrafts::getMailId,mailId)
				.in(SysMessageDrafts::getState,list);
		if (StringUtils.isNotBlank(colomn) && StringUtils.isNotBlank(word)) {
			colomn = colomn.trim();
			word = word.trim();
			if ("subject".equalsIgnoreCase(colomn)) {
				wrapper.like(SysMessageDrafts::getSubject,word);
			} else if ("bodyText".equalsIgnoreCase(colomn)) {
				wrapper.like(SysMessageDrafts::getBodyText,word);
			} else if ("mailFrom".equalsIgnoreCase(colomn)) {
				wrapper.like(SysMessageDrafts::getMailFrom,word);
			} else if ("mailAddress".equalsIgnoreCase(colomn)) {
				wrapper.like(SysMessageDrafts::getMailAddress,word);
			}
		}
		return sysMessageDraftsMapper.selectCount(wrapper);
	}

	@Override
	public List<SysMessageDrafts> listByPage(long userId, String colomn, String word, int offset, int limit,
			int... states) {
		// TODO Auto-generated method stub
		logger.info("class:" + this.getClass().getName() + " method:" + "listByPage" + " param:" + "userId:" + userId);
		List<Integer> list = new ArrayList<Integer>();
		for (int i = 0; i < states.length; i++) {
			list.add(states[i]);
		}
		LambdaQueryWrapper<SysMessageDrafts> wrapper =Wrappers.lambdaQuery(SysMessageDrafts.class)
				.eq(SysMessageDrafts::getUserId,userId)
				.in(SysMessageDrafts::getState,list);
		if (StringUtils.isNotBlank(colomn) && StringUtils.isNotBlank(word)) {
			colomn = colomn.trim();
			word = word.trim();
			if ("subject".equalsIgnoreCase(colomn)) {
				wrapper.like(SysMessageDrafts::getSubject,word);
			} else if ("bodyText".equalsIgnoreCase(colomn)) {
				wrapper.like(SysMessageDrafts::getBodyText,word);
			} else if ("mailFrom".equalsIgnoreCase(colomn)) {
				wrapper.like(SysMessageDrafts::getMailFrom,word);
			} else if ("mailAddress".equalsIgnoreCase(colomn)) {
				wrapper.like(SysMessageDrafts::getMailAddress,word);
			}
		}
		Page<SysMessageDrafts> page = new Page<>();
		page.setCurrent(offset);
		page.setSize(limit);
		return sysMessageDraftsMapper.selectPage(page,wrapper).getRecords();
	}

	@Override
	public long count(long userId, String colomn, String word, int... states) {
		// TODO Auto-generated method stub
		logger.info("class:" + this.getClass().getName() + " method:" + "count" + " param:" + "userId:" + userId);
		List<Integer> list = new ArrayList<Integer>();
		for (int i = 0; i < states.length; i++) {
			list.add(states[i]);
		}
		LambdaQueryWrapper<SysMessageDrafts> wrapper =Wrappers.lambdaQuery(SysMessageDrafts.class)
				.eq(SysMessageDrafts::getUserId,userId)
				.in(SysMessageDrafts::getState,list);
		if (StringUtils.isNotBlank(colomn) && StringUtils.isNotBlank(word)) {
			colomn = colomn.trim();
			word = word.trim();
			if ("subject".equalsIgnoreCase(colomn)) {
				wrapper.like(SysMessageDrafts::getSubject,word);
			} else if ("bodyText".equalsIgnoreCase(colomn)) {
				wrapper.like(SysMessageDrafts::getBodyText,word);
			} else if ("mailFrom".equalsIgnoreCase(colomn)) {
				wrapper.like(SysMessageDrafts::getMailFrom,word);
			} else if ("mailAddress".equalsIgnoreCase(colomn)) {
				wrapper.like(SysMessageDrafts::getMailAddress,word);
			}
		}
		return sysMessageDraftsMapper.selectCount(wrapper);
	}
}
